#include "udp_server.hpp"
#include <memory>
using namespace udp_server;
using namespace std;

static void usage(char* proc)
{
    cout << "Usage:\n\t" << proc << " port\n" << endl;
}

std::string transactionString(std::string request)
{
    std::string result;
    char c;
    for (auto& r : request)
    {
        if (islower(r))
        {
            c = toupper(r);
            result.push_back(c);
        }
        else
        {
            result.push_back(r);
        }
    }
    return result;
}

static bool isPass(string command)
{
    bool ret = true;
    auto pos = command.find("rm");
    if (pos != string::npos) ret = false;
    pos = command.find("mv");
    if (pos != string::npos) ret = false;

    return ret;
}

std::string excuteCommand(std::string command)
{
    if (!isPass(command)) return "有坏人";

    FILE* fp = popen(command.c_str(), "r");
    if (fp == nullptr) return "None";
    
    char line[1024];
    string result;
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        result += line;
    }
    pclose(fp);
    return result;
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);
    //unique_ptr<UdpServer> usvr(new UdpServer(transactionString, port));
    unique_ptr<UdpServer> usvr(new UdpServer(port));
    usvr->start();
    return 0;
}